package com.zmx.patients.view.activity;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.zmx.patients.BaseActivity;
import com.zmx.patients.R;
import com.zmx.patients.adapter.GoodsClassificationAdapter;
import com.zmx.patients.adapter.MallGoodsAdapter;
import com.zmx.patients.okhttp.OkHttp3ClientManager;
import com.zmx.patients.pojo.BannerMessage;
import com.zmx.patients.pojo.GoodsClassification;
import com.zmx.patients.pojo.MallGoods;
import com.zmx.patients.utils.AllUtil;
import com.zmx.patients.view.util.GlideImageLoader;
import com.zmx.patients.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 时间：2020/5/3 19:24
 * 功能：药在线
 */

public class MallMedicineActivity extends BaseActivity {

    @BindView(R.id.logo)
    TextView logo;
    @BindView(R.id.message_button)
    ImageView messageButton;
    @BindView(R.id.goods_image_a)
    ImageView goodsImageA;
    @BindView(R.id.goods_image_b)
    ImageView goodsImageB;
    @BindView(R.id.goods_image_c)
    ImageView goodsImageC;
    private GoodsClassificationAdapter adapter;
    private List<GoodsClassification> lists;

    private MallGoodsAdapter mgAdapter;
    private MallGoodsAdapter r_mgAdapter;
    private List<MallGoods> mallGoods;
    private List<MallGoods> r_Goods;
    private List<BannerMessage> bms;

    private RecyclerView grid_view;
    private RecyclerView r_View;
    private RecyclerView recyclerView;

    private Banner banner;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_mall_medicine;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

        grid_view = findViewById(R.id.grid_view);
        LinearLayoutManager layout = new LinearLayoutManager(this);
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);//设置为横向排列
        grid_view.setLayoutManager(layout);
        lists = new ArrayList<>();
        adapter = new GoodsClassificationAdapter(mActivity);
        grid_view.setAdapter(adapter);
        bms = new ArrayList<>();

        recyclerView = findViewById(R.id.recyclerView);
        mgAdapter = new MallGoodsAdapter(mActivity);
        recyclerView.setLayoutManager(new GridLayoutManager(mActivity, 3));
        recyclerView.setAdapter(mgAdapter);
        mallGoods = new ArrayList<>();
        mgAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                Intent intent = new Intent(mActivity, GoodsDetailsActivity.class);
                intent.putExtra("goods_id", mallGoods.get(listPosition).getGoods_id() + "");
                startActivity(intent);
            }
        });

        r_View = findViewById(R.id.r_View);
        r_mgAdapter = new MallGoodsAdapter(mActivity);
        LinearLayoutManager layouts = new LinearLayoutManager(this);
        layouts.setOrientation(LinearLayoutManager.HORIZONTAL);//设置为横向排列
        r_View.setLayoutManager(layouts);
        r_View.setAdapter(r_mgAdapter);
        r_Goods = new ArrayList<>();
        r_mgAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                Intent intent = new Intent(mActivity, GoodsDetailsActivity.class);
                intent.putExtra("goods_id", mallGoods.get(listPosition).getGoods_id() + "");
                startActivity(intent);
            }
        });

        banner = findViewById(R.id.banner);


        getGroup();
        getCarousel();
        getActivity();
        getIndex();
        getActivity_r();


    }


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());

                        JSONArray data = object.getJSONArray("data");
                        Gson g = new Gson();
                        for (int i = 0; i < data.length(); i++) {

                            JSONObject jsonObject = data.getJSONObject(i);
                            GoodsClassification gc = g.fromJson(jsonObject.toString(), GoodsClassification.class);
                            lists.add(gc);

                        }

                        adapter.setListAndNotifyDataSetChanged(lists);


                    } catch (JSONException e) {
                        Log.e("异常", "异常" + e.toString());
                        e.printStackTrace();
                    }

                    break;

                case 2:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray data = object.getJSONArray("data");
                        Gson g = new Gson();

                        for (int i = 0; i < data.length(); i++) {

                            JSONObject jsonObject = data.getJSONObject(i);
                            MallGoods gc = g.fromJson(jsonObject.toString(), MallGoods.class);
                            mallGoods.add(gc);

                        }

                        mgAdapter.setListAndNotifyDataSetChanged(mallGoods);

                    } catch (JSONException e) {
                        Log.e("异常", "异常" + e.toString());
                        e.printStackTrace();
                    }

                    break;

                case 3:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());

                        List<String> images = new ArrayList<>();
                        JSONArray data = object.getJSONArray("data");
                        Gson g = new Gson();
                        for (int i = 0; i < data.length(); i++) {

                            JSONObject jsonObject = data.getJSONObject(i);
                            BannerMessage gc = g.fromJson(jsonObject.toString(), BannerMessage.class);
                            bms.add(gc);
                            images.add(gc.getPicture());

                        }

                        //设置图片加载器😀+
                        banner.setImageLoader(new GlideImageLoader());
                        //设置图片集合
                        banner.setDelayTime(3000);
                        banner.setImages(images);
                        banner.setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(int position) {

                                Intent intent = new Intent(mActivity, GoodsDetailsActivity.class);
                                intent.putExtra("goods_id", bms.get(position).getGoods_id() + "");
                                startActivity(intent);

                            }
                        });
                        //banner设置方法全部调用完毕时最后调用
                        banner.start();

                    } catch (JSONException e) {
                        Log.e("异常", "异常" + e.toString());
                        e.printStackTrace();
                    }

                    break;

                case 4:

                    try {
                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray data = object.getJSONArray("data");
                        Gson g = new Gson();

                        for (int i = 0; i < data.length(); i++) {

                            JSONObject jsonObject = data.getJSONObject(i);
                            MallGoods gc = g.fromJson(jsonObject.toString(), MallGoods.class);

                            if (i == 0) {

                                Glide.with(mActivity).load(gc.getImg()).into(goodsImageA);

                            }else if (i == 1){

                                Glide.with(mActivity).load(gc.getImg()).into(goodsImageB);

                            }else{
                                Glide.with(mActivity).load(gc.getImg()).into(goodsImageC);
                            }


                        }


                    } catch (JSONException e) {
                        Log.e("异常", "异常" + e.toString());
                        e.printStackTrace();
                    }

                    break;

                case 5:

                    try {

                        Log.e("异常", "异常fdsfasffa" + msg.obj.toString());
                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray data = object.getJSONArray("data");
                        Gson g = new Gson();

                        for (int i = 0; i < data.length(); i++) {

                            JSONObject jsonObject = data.getJSONObject(i);
                            MallGoods gc = g.fromJson(jsonObject.toString(), MallGoods.class);
                            r_Goods.add(gc);

                        }

                        r_mgAdapter.setListAndNotifyDataSetChanged(r_Goods);

                    } catch (JSONException e) {
                        Log.e("异常", "异常" + e.toString());
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    public void getGroup() {

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getGroup");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/mall/", params, handler, 1, 404);

    }

    public void getCarousel() {

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getCarousel");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/mall/", params, handler, 3, 404);

    }

    public void getActivity() {

        List<String> l = new ArrayList<>();
        l.add("2");
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getActivity");
        params.put("data[type]", "2");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/mall/", params, handler, 2, 404);

    }
    public void getActivity_r() {

        List<String> l = new ArrayList<>();
        l.add("1");
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getActivity");
        params.put("data[type]", "1");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/mall/", params, handler, 5, 404);

    }
    public void getIndex() {

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getIndex");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/mall/", params, handler, 4, 404);

    }


    @OnClick({R.id.logo, R.id.message_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.logo:

                Bundle bundle = new Bundle();
                bundle.putString("logo", "0");
                startActivity(OrderManagementActivity.class, bundle);

                break;

            case R.id.message_button:

                startActivity(ShoppingCartActivity.class);

                break;

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
